package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.bo.WxCommentListBO;
import com.cskaoyan.app.bean.bo.WxCommentPostBO;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketComment;
import com.cskaoyan.app.bean.po.MarketCommentCount;
import com.cskaoyan.app.bean.po.MarketCommentExample;
import com.cskaoyan.app.bean.po.MarketCommentUser;
import com.cskaoyan.app.bean.vo.WxCommentCountVO;
import com.cskaoyan.app.mapper.MarketCommentMapper;
import com.cskaoyan.app.mapper.MarketUserMapper;
import com.cskaoyan.app.wxservice.WeChatCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Shuuka
 * @email 1712430536@qq.com
 * @date 2023/1/2 9:51
 */
@Service
@Transactional
public class WeChatCommentServiceImpl implements WeChatCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public WxCommentCountVO count(Integer valueId, Integer type) {
        List<MarketCommentCount> marketCommentCountList = marketCommentMapper.commentCountPicture(valueId, type);
        WxCommentCountVO wxCommentCountVO = new WxCommentCountVO();
        int count = 0;
        for (MarketCommentCount marketCommentCount : marketCommentCountList) {
            count += marketCommentCount.getCount();
            if (marketCommentCount.getHasPicture()) {
                wxCommentCountVO.setHasPicCount(marketCommentCount.getCount());
            }
        }
        wxCommentCountVO.setAllCount(count);
        return wxCommentCountVO;
    }

    @Override
    public CommonData<MarketComment> list(WxCommentListBO wxCommentListBO) {
        PageHelper.startPage(wxCommentListBO.getPage(), wxCommentListBO.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        example.setOrderByClause("update_time DESC");
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(wxCommentListBO.getValueId());
        criteria.andTypeEqualTo(wxCommentListBO.getType());
        if (wxCommentListBO.getShowType()) {
            criteria.andHasPictureEqualTo(wxCommentListBO.getShowType());
        }
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);
        //用stream将不重复id筛选出来
        List<Integer> collect = marketComments.stream().map(MarketComment::getUserId).distinct().collect(Collectors.toList());
        System.out.println(collect + "--------**##@@#@$*(@@*&%");
        List<MarketCommentUser> wxCommentUsers = null;
        Map<Integer, String> idNicknameMap = null;
        Map<Integer, String> idUrlMap = null;

        //判空逻辑存在一定问题

        if (collect.size() != 0) {
            wxCommentUsers = marketUserMapper.selectNameUrlbyUserID(collect);
            idNicknameMap = wxCommentUsers.stream().collect(Collectors.toMap(MarketCommentUser::getId, MarketCommentUser::getNickName));
            idUrlMap = wxCommentUsers.stream().collect(Collectors.toMap(MarketCommentUser::getId, MarketCommentUser::getAvatarUrl));
        }
        for (MarketComment marketComment : marketComments) {
            marketComment.setPicList(marketComment.getPicUrls());
            Integer userId = marketComment.getUserId();
            marketComment.setUserInfo(new MarketCommentUser());
            if(wxCommentUsers!= null){
                marketComment.setUserInfo(new MarketCommentUser(userId, idNicknameMap.get(userId), idUrlMap.get(userId)));
            }
        }
        PageInfo<MarketComment> pageInfo = new PageInfo<>(marketComments);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketComment post(WxCommentPostBO wxCommentPostBO, Integer userid) {
        MarketComment marketComment = new MarketComment();
        Date now = new Date();
        marketComment.setValueId(Integer.parseInt(wxCommentPostBO.getValueId()));
        marketComment.setType(wxCommentPostBO.getType());
        marketComment.setContent(wxCommentPostBO.getContent());
        marketComment.setUserId(userid);
        marketComment.setHasPicture(wxCommentPostBO.getHasPicture());
        marketComment.setPicUrls(wxCommentPostBO.getPicUrls());
        marketComment.setStar(wxCommentPostBO.getStar());
        marketComment.setAddTime(now);
        marketComment.setUpdateTime(now);
        marketComment.setDeleted(false);
        Integer row = marketCommentMapper.insertSelective(marketComment);
        marketComment = marketCommentMapper.selectByPrimaryKey(marketComment.getId());
        return marketComment;
    }


}
